package com.twu.bm.football;

import org.junit.Test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintStream;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class SearchOptionTest {

    PrintStream printStream = mock(PrintStream.class);
    BufferedReader reader = mock(BufferedReader.class);
    League leagueMock = mock(League.class);
    String searchMessage = "To search for a player, enter a name, then enter a number:";
    String playerName = "John Smith";
    String playerNumber = "13";

    SearchOption searchOption = new SearchOption(printStream, reader, leagueMock);

    @Test
    public void shouldPrintSearchMessageWhenActionPerformed() {
        searchOption.performAction();
        verify(printStream).println(searchMessage);
    }

    @Test
    public void shouldPrintNotFoundMessageWhenBadSearchInputIsEntered() throws IOException {
        when(reader.readLine())
                .thenReturn("John Doe")
                .thenReturn("13");

        searchOption.performAction();

        verify(printStream).println("No match found!");
    }

    @Test
    public void shouldCallMatchPlayerWhenInputIsEntered() throws IOException {
        when(reader.readLine())
                .thenReturn(playerName)
                .thenReturn(playerNumber);

        searchOption.performAction();
        verify(leagueMock).matchPlayer(playerName, playerNumber);
    }

    @Test
    public void shouldPrintStackTraceWhenIOExceptionThrown() throws IOException {
        IOException exMock = mock(IOException.class);

        when(reader.readLine())
                .thenThrow(exMock);

        searchOption.performAction();
        verify(exMock).printStackTrace();
    }

    @Test
    public void shouldPrintPlayerWhenValidSearchInputIsEntered() throws IOException {
        Player playerMock = mock(Player.class);

        when(reader.readLine())
                .thenReturn(playerName)
                .thenReturn(playerNumber);
        when(leagueMock.matchPlayer(playerName, playerNumber))
                .thenReturn(playerMock);

        searchOption.performAction();
        verify(playerMock).print(printStream);
    }


}
